package com.ikingtech.platform.service.attachment.controller;

import com.ikingtech.framework.sdk.attachment.api.AttachmentApi;
import com.ikingtech.framework.sdk.attachment.model.AttachmentDTO;
import com.ikingtech.framework.sdk.attachment.model.AttachmentQueryParamDTO;
import com.ikingtech.framework.sdk.base.model.BatchParam;
import com.ikingtech.framework.sdk.context.exception.FrameworkException;
import com.ikingtech.framework.sdk.core.response.R;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.framework.sdk.web.annotation.ApiController;
import com.ikingtech.framework.sdk.web.annotation.PostRequest;
import com.ikingtech.platform.service.attachment.entity.AttachmentDO;
import com.ikingtech.platform.service.attachment.service.AttachmentService;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author tie yan
 */
@CrossOrigin
@RequiredArgsConstructor
@ApiController(value = "/attachment", name = "附件", description = "附件")
public class AttachmentController implements AttachmentApi {

    private final AttachmentService service;

    @Transactional(rollbackFor = Exception.class)
    @PostRequest(order = 1, value = "/save-or-update/batch", summary = "批量更新", description = "批量更新")
    public R<Object> saveOrUpdate(@Parameter(name = "attachment", description = "新增参数")
                                       @RequestBody AttachmentDTO attachments) {
        AttachmentDO entity = Tools.Bean.copy(attachments, AttachmentDO.class);
        entity.setId(Tools.Id.uuid());
        this.service.save(entity);
        return R.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostRequest(order = 2, value = "/delete/batch", summary = "批量删除(业务编号集合)", description = "批量删除(业务编号集合)")
    public R<Object> deleteBatch(@Parameter(name = "ids", description = "删除参数")
                                 @RequestBody BatchParam<String> businessIds) {
        this.service.removeById(businessIds);
        return R.ok();
    }

    @PostRequest(order = 4, value = "/detail", summary = "详情", description = "详情")
    public R<AttachmentDTO> detail(@Parameter(name = "id", description = "主键编号")
                                   @RequestBody String id) {
        AttachmentDO entity = this.service.getById(id);
        if (null == entity) {
            throw new FrameworkException("对象不存在");
        }
        AttachmentDTO attachmentFile = Tools.Bean.copy(entity, AttachmentDTO.class);
        return R.ok(attachmentFile);
    }

    @PostRequest(order = 5, value = "/list/page", summary = "分页查询", description = "分页查询")
    public R<List<AttachmentDTO>> page(@Parameter(name = "queryParam", description = "查询参数")
                                       @RequestBody AttachmentQueryParamDTO queryParam) {
        return R.ok(this.service.listPage(queryParam).convert(entity -> Tools.Bean.copy(entity, AttachmentDTO.class)));
    }

    @PostRequest(order = 6, value = "/list/all", summary = "全量查询", description = "全量查询")
    public R<List<AttachmentDTO>> all() {
        return R.ok(Tools.Coll.convertList(this.service.list(), entity -> Tools.Bean.copy(entity, AttachmentDTO.class)));
    }

    @PostRequest(order = 6, value = "/list/business-id", summary = "查询附件(业务编号)", description = "查询附件(业务编号)")
    public R<List<AttachmentDTO>> listByBusinessId(String businessId) {
        return R.ok(Tools.Coll.convertList(this.service.list(), entity -> Tools.Bean.copy(entity, AttachmentDTO.class)));
    }

    @PostRequest(order = 6, value = "/list/business-ids", summary = "查询附件(业务编号集合)", description = "查询附件(业务编号集合)")
    public R<List<AttachmentDTO>> listByBusinessIds(BatchParam<String> businessIds) {
        return R.ok(Tools.Coll.convertList(this.service.list(), entity -> Tools.Bean.copy(entity, AttachmentDTO.class)));
    }
}